// Copyright  2004-2008 Backplane Ltd.
//
// This file is part of Ubiquity formsPlayer
//
// Ubiquity formsPlayer is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
//
// Ubiquity formsPlayer is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License
// along with Ubiquity formsPlayer If not, see <http://www.gnu.org/licenses/>.
//	
//
//   Purpose: Implementation of CImplActionLoadURI.
//   $Id$

#include "stdafx.h"
#include "actionload.h"
#include "xformselementload.h"
#include "XFormsXFormsElement.h"
#include "singlenodebindingondemand.h"
#include "ImplContextualValue.h"
#include "ImplBind.h"


CImplActionLoadURI::CImplActionLoadURI(CXFormsElementLoad *pXFEl):
	m_eShow(EXlinkShow::replace),m_pBind(0),m_pRefEvaluator(0),m_pResourceResolver(0)
{
	m_pXFormsElement = pXFEl;
	pXFEl->AddRef();
}


CImplActionLoadURI::CImplActionLoadURI():
	m_eShow(EXlinkShow::replace),m_pBind(0),m_pRefEvaluator(0),m_pResourceResolver(0)
{
	
}


CImplActionLoadURI::~CImplActionLoadURI()
{
	delete m_pRefEvaluator;
	delete m_pResourceResolver;
	m_pXFormsElement->formControlUnInitialize();
}


HRESULT CImplActionLoadURI::PerformAction()
{
	HRESULT hr = E_FAIL;
	//resource element takes ultimate precedence
	if(m_pResourceResolver)
	{
		ResolveEvaluationContext();
	}
	else if(!m_sHref)
	{
		ResolveEvaluationContext();

		//bind takes precedence
		if(m_sBind)
		{	
			if(m_pXForms)
				m_pXForms->getBind(m_sBind,m_pBind);
		}
		else if(m_sRef)
		{
			if(m_pModel && !m_pRefEvaluator)
				m_pRefEvaluator = new CSingleNodeBindingOnDemand(m_pModel,m_sRef);
		}
	}

	//"Either the single node binding attributes, 
	//	pointing to a URI in the instance data, 
	//	or the linking attributes are required. 
	//	If both are present, the action has no effect."
	if((!m_sBind || !m_sRef) || !m_sHref)
	{
		if(m_pResourceResolver)
		{
			CComBSTR sResource;
			CComPtr<IXMLDOMNode> pContext;
			GetXPathContext(&pContext);
			m_pResourceResolver->GetValue(m_pModel,pContext,&sResource);
			m_pXFormsElement->put_value(sResource);
		}
		else if(m_sHref)
			hr = m_pXFormsElement->put_value(m_sHref);
		else if(m_pBind)
		{
			CComBSTR sHref;
			CComPtr<IXMLDOMNode> pNode;
			m_pBind->GetNodeAt(0,&pNode);
			if(pNode)
			{
				pNode->get_text(&sHref);
				hr = m_pXFormsElement->put_value(sHref);
			}
		}
		else
		{
			CComPtr<IXMLDOMNode> pContext;
			GetXPathContext(&pContext);
			if(pContext)
			{
				CComBSTR sHref;
				if(m_pRefEvaluator)
				{
					CComPtr<IXMLDOMNode> pNode;
					m_pRefEvaluator->GetNode(pContext,&pNode);
					if(pNode)
					{
						pNode->get_text(&sHref);
						if(sHref)
							hr = m_pXFormsElement->put_value(sHref);
					}
				}
				
			}
		}
	}
	
	// Bug 828: Allow dynamic invocation of Soft-Bars.
	if(m_eShow == softbar)
		hr = E_NOTIMPL;
	else if(m_eShow == systray) {
		hr = E_NOTIMPL;
	}

	if(m_pXFormsElement)
		FireEvent(EVENT_ACTIVATE);


	return hr;
}



void CImplActionLoadURI::ResolveBindings() 
{
	

}
